package View.User;

import Dao.UserDao;
import Service.ServiceImpl.UserServiceImpl;
import Service.UserService;
import View.Index.Index_admin;
import domain.User;
import org.springframework.core.style.StylerUtils;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class QueryUser extends JFrame {

    public QueryUser(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("查询用户信息");
        this.setSize(933,518);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("id:");
        label1.setBounds(40,18,65,30);
        JLabel label2= new JLabel();
        label2.setText("用户名:");
        label2.setBounds(222,18,65,30);
        JLabel label3= new JLabel();
        label3.setText("用户类型:");
        label3.setBounds(422,19,65,30);
        JLabel label4= new JLabel();
        label4.setText("学号");
        label4.setBounds(623,20,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(90,18,120,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(293,19,120,30);
        JTextField field3= new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(493,19,120,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(699,20,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("查询");
        button1.setBounds(200,67,175,43);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(500,67,175,43);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(100,120,700,350);




        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(button1);
        this.add(button2);
        this.add(panel);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //存放表格的字段
                String title[] = {"id","用户名","用户类型","学生号"};
                if(!"".equals(field1) && !"".equals(field2) && !"".equals(field3) && !"".equals(field4)) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("id", field1.getText());
                    map.put("username", field2.getText());
                    map.put("type", field3.getText());
                    map.put("stunum", field4.getText());

                    UserService service = new UserServiceImpl();
                    List<User> users = service.fuzztSelectUser(map);


                    //存放表格的内容
                    Object[][] neirong = new Object[users.size()][4];
                    for (int i = 0; i < users.size(); i++) {
                        User user = users.get(i);
                        neirong[i][0] = user.getId();
                        neirong[i][1] = user.getUsername();
                        neirong[i][2] = user.getType();
                        neirong[i][3] = user.getStunum();
                    }

                    //查询结果如下
                    JTable table = new JTable(neirong, title);
                    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                    tcr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, tcr);
                    panel.setViewportView(table);
                }else {
                    //如果文本框什么都没有输入就直接把表中所有的内容展示
                    UserService service = new UserServiceImpl();
                    List<User> users = service.userList();

                    Object[][] neirong = new Object[users.size()][4];
                    for (int i = 0; i < users.size(); i++) {
                        User user = users.get(i);
                        neirong[i][0] = user.getId();
                        neirong[i][1] = user.getUsername();
                        neirong[i][2] = user.getType();
                        neirong[i][3] = user.getStunum();
                    }

                    JTable table = new JTable(neirong, title);
                    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                    tcr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, tcr);
                    panel.setViewportView(table);
                }

            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }

}
